home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / md5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  12.2 KB  |  417 lines

  1. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  2.  */
  3.  
  4. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  5. rights reserved.
  6.  
  7. License to copy and use this software is granted provided that it
  8. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  9. Algorithm" in all material mentioning or referencing this software
  10. or this function.
  11.  
  12. License is also granted to make and use derivative works provided
  13. that such works are identified as "derived from the RSA Data
  14. Security, Inc. MD5 Message-Digest Algorithm" in all material
  15. mentioning or referencing the derived work.
  16.  
  17. RSA Data Security, Inc. makes no representations concerning either
  18. the merchantability of this software or the suitability of this
  19. software for any particular purpose. It is provided "as is"
  20. without express or implied warranty of any kind.
  21.  
  22. These notices must be retained in any copies of any part of this
  23. documentation and/or software.
  24.  */
  25.  
  26. #include <config.h>
  27. #include "lisp.h"
  28.  
  29. #include "buffer.h"
  30. #include "insdel.h"
  31.  
  32. typedef unsigned char *POINTER;/* POINTER defines a generic pointer type */
  33. typedef unsigned short int UINT2;/* UINT2 defines a two byte word */
  34. typedef unsigned long  int UINT4;/* UINT4 defines a four byte word */
  35.  
  36. #define PROTO_LIST(list) list
  37. #define MD_CTX MD5_CTX
  38. #define MDInit MD5Init
  39. #define MDUpdate MD5Update
  40. #define MDFinal MD5Final
  41.  
  42. /* MD5 context. */
  43. typedef struct {
  44.   UINT4 state[4];                                   /* state (ABCD) */
  45.   UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
  46.   unsigned char buffer[64];                         /* input buffer */
  47. } MD5_CTX;
  48.  
  49. void MD5Init PROTO_LIST ((MD5_CTX *));
  50. void MD5Update PROTO_LIST
  51.   ((MD5_CTX *, unsigned char *, unsigned int));
  52. void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
  53.  
  54. /* Constants for MD5Transform routine.
  55.  */
  56. #define S11 7
  57. #define S12 12
  58. #define S13 17
  59. #define S14 22
  60. #define S21 5
  61. #define S22 9
  62. #define S23 14
  63. #define S24 20
  64. #define S31 4
  65. #define S32 11
  66. #define S33 16
  67. #define S34 23
  68. #define S41 6
  69. #define S42 10
  70. #define S43 15
  71. #define S44 21
  72.  
  73. static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  74. static void Encode PROTO_LIST
  75.   ((unsigned char *, UINT4 *, unsigned int));
  76. static void Decode PROTO_LIST
  77.   ((UINT4 *, unsigned char *, unsigned int));
  78. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  79. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  80.  
  81. static unsigned char PADDING[64] = {
  82.   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  85. };
  86.  
  87. /* F, G, H and I are basic MD5 functions.
  88.  */
  89. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  90. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  91. #define H(x, y, z) ((x) ^ (y) ^ (z))
  92. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  93.  
  94. /* ROTATE_LEFT rotates x left n bits.
  95.  */
  96. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  97.  
  98. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  99. Rotation is separate from addition to prevent recomputation.
  100.  */
  101. #define FF(a, b, c, d, x, s, ac) { \
  102.  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  103.  (a) = ROTATE_LEFT ((a), (s)); \
  104.  (a) += (b); \
  105.   }
  106. #define GG(a, b, c, d, x, s, ac) { \
  107.  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  108.  (a) = ROTATE_LEFT ((a), (s)); \
  109.  (a) += (b); \
  110.   }
  111. #define HH(a, b, c, d, x, s, ac) { \
  112.  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  113.  (a) = ROTATE_LEFT ((a), (s)); \
  114.  (a) += (b); \
  115.   }
  116. #define II(a, b, c, d, x, s, ac) { \
  117.  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  118.  (a) = ROTATE_LEFT ((a), (s)); \
  119.  (a) += (b); \
  120.   }
  121.  
  122. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  123.  */
  124. void
  125. MD5Init (MD5_CTX *context)
  126. {
  127.   context->count[0] = context->count[1] = 0;
  128.  
  129.   /* Load magic initialization constants. */
  130.   context->state[0] = 0x67452301;
  131.   context->state[1] = 0xefcdab89;
  132.   context->state[2] = 0x98badcfe;
  133.   context->state[3] = 0x10325476;
  134. }
  135.  
  136. /* MD5 block update operation. Continues an MD5 message-digest
  137.   operation, processing another message block, and updating the
  138.   context.
  139.  */
  140. void
  141. MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen)
  142. {
  143.   unsigned int i, index, partLen;
  144.  
  145.   /* Compute number of bytes mod 64 */
  146.   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  147.  
  148.   /* Update number of bits */
  149.   if ((context->count[0] += ((UINT4)inputLen << 3))
  150.       < ((UINT4)inputLen << 3))
  151.     context->count[1]++;
  152.   context->count[1] += ((UINT4)inputLen >> 29);
  153.  
  154.   partLen = 64 - index;
  155.  
  156.   /* Transform as many times as possible. */
  157.   if (inputLen >= partLen)
  158.     {
  159.       MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  160.       MD5Transform (context->state, context->buffer);
  161.  
  162.       for (i = partLen; i + 63 < inputLen; i += 64)
  163.     MD5Transform (context->state, &input[i]);
  164.  
  165.       index = 0;
  166.     }
  167.   else
  168.     i = 0;
  169.  
  170.   /* Buffer remaining input */
  171.   MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i],
  172.           inputLen-i);
  173. }
  174.  
  175. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  176.   the message digest and zeroizing the context.
  177.  */
  178. void
  179. MD5Final (unsigned char digest[16], MD5_CTX *context)
  180. {
  181.   unsigned char bits[8];
  182.   unsigned int index, padLen;
  183.  
  184.   /* Save number of bits */
  185.   Encode (bits, context->count, 8);
  186.  
  187.   /* Pad out to 56 mod 64.
  188. */
  189.   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  190.   padLen = (index < 56) ? (56 - index) : (120 - index);
  191.   MD5Update (context, PADDING, padLen);
  192.  
  193.   /* Append length (before padding) */
  194.   MD5Update (context, bits, 8);
  195.   /* Store state in digest */
  196.   Encode (digest, context->state, 16);
  197.  
  198.   /* Zeroize sensitive information.
  199. */
  200.   MD5_memset ((POINTER)context, 0, sizeof (*context));
  201. }
  202.  
  203. /* MD5 basic transformation. Transforms state based on block.
  204.  */
  205. static void
  206. MD5Transform (UINT4 state[4], unsigned char block[64])
  207. {
  208.   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  209.  
  210.   Decode (x, block, 64);
  211.  
  212.   /* Round 1 */
  213.   FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  214.   FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  215.   FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  216.   FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  217.   FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  218.   FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  219.   FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  220.   FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  221.   FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  222.   FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  223.   FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  224.   FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  225.   FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  226.   FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  227.   FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  228.   FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  229.  
  230.  /* Round 2 */
  231.   GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  232.   GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  233.   GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  234.   GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  235.   GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  236.   GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  237.   GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  238.   GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  239.   GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  240.   GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  241.   GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  242.   GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  243.   GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  244.   GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  245.   GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  246.   GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  247.  
  248.   /* Round 3 */
  249.   HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  250.   HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  251.   HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  252.   HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  253.   HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  254.   HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  255.   HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  256.   HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  257.   HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  258.   HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  259.   HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  260.   HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  261.   HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  262.   HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  263.   HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  264.   HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  265.  
  266.   /* Round 4 */
  267.   II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  268.   II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  269.   II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  270.   II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  271.   II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  272.   II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  273.   II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  274.   II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  275.   II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  276.   II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  277.   II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  278.   II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  279.   II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  280.   II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  281.   II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  282.   II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  283.  
  284.   state[0] += a;
  285.   state[1] += b;
  286.   state[2] += c;
  287.   state[3] += d;
  288.  
  289.   /* Zeroize sensitive information.
  290. */
  291.   MD5_memset ((POINTER)x, 0, sizeof (x));
  292. }
  293.  
  294. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  295.   a multiple of 4.
  296.  */
  297. static void
  298. Encode (unsigned char *output, UINT4 *input, unsigned int len)
  299. {
  300.   unsigned int i, j;
  301.  
  302.   for (i = 0, j = 0; j < len; i++, j += 4)
  303.     {
  304.       output[j] = (unsigned char)(input[i] & 0xff);
  305.       output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  306.       output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  307.       output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  308.     }
  309. }
  310.  
  311. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  312.   a multiple of 4.
  313.  */
  314. static void
  315. Decode (UINT4 *output, unsigned char *input, unsigned int len)
  316. {
  317.   unsigned int i, j;
  318.  
  319.   for (i = 0, j = 0; j < len; i++, j += 4)
  320.     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  321.       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  322. }
  323.  
  324. static void
  325. MD5_memcpy (POINTER output, POINTER input, unsigned int len)
  326. {
  327.   memcpy (output, input, len);
  328. }
  329.  
  330. static void
  331. MD5_memset (POINTER output, int value, unsigned int len)
  332. {
  333.   memset (output, value, len);
  334. }
  335.  
  336. /* unused */
  337. #if 0
  338. static void
  339. LispMDString (char *string)
  340. {
  341.   MD_CTX context;
  342.   unsigned char digest[16];
  343.   unsigned int len = strlen(string);
  344.  
  345.   MDInit (&context);
  346.   MDUpdate (&context, string, len);
  347.   MDFinal (digest, &context);
  348. }
  349. #endif
  350.  
  351.  
  352. /* XEmacs interface code. */
  353. Lisp_Object Qmd5;
  354.  
  355. DEFUN ("md5", Fmd5, Smd5, 1, 3, 0,
  356.        "Return the MD5 (a secure message digest algorithm) of an object.\n\
  357. OBJECT is either a string or a buffer.\n\
  358. Optional arguments START and END denote buffer positions for computing the\n\
  359. hash of a portion of OBJECT.")
  360.        (object, start, end)
  361.        Lisp_Object object, start, end;
  362. {
  363.   MD_CTX context;
  364.   unsigned char digest[16];
  365.   unsigned char thehash[32];
  366.   int i;
  367.   
  368.   MDInit (&context);
  369.  
  370.   if (NILP (object))
  371.     {
  372.       MDUpdate (&context, (unsigned char *) "", 0);
  373.     }
  374.   else if (BUFFERP (object))
  375.     {
  376.       struct buffer *b = decode_buffer (object, 1);
  377.       Bufpos begv, endv;
  378.       Lisp_Object string;
  379.  
  380.       /* Figure out where we need to get info from */
  381.       get_bufrange (b, start, end, &begv, &endv, GB_ALLOW_NIL);
  382.  
  383.       /* Get the string data from the buffer */
  384.       string = make_string_from_buffer (b, begv, endv-begv);
  385.  
  386.     /* Compute the digest */
  387.       MDUpdate (&context, (unsigned char *) string_data (XSTRING(string)),
  388.         endv-begv);
  389.     }
  390.   else
  391.     {
  392.       Bytecount len, bstart, bend;
  393.       len = get_string_range (object, start, end, &bstart, &bend);
  394.       MDUpdate (&context, ((unsigned char *) string_data (XSTRING(object))
  395.                + bstart), len);
  396.     }
  397.  
  398.   MDFinal (digest, &context);
  399.   for (i = 0; i < 16; i++)
  400.     sprintf ((char *) (thehash + (i * 2)), "%02x", digest[i]);
  401.  
  402.   return (make_string (thehash, 32));
  403. }
  404.  
  405. void
  406. syms_of_md5 (void)
  407. {
  408.   defsubr (&Smd5);
  409.   defsymbol (&Qmd5, "md5");
  410. }
  411.  
  412. void
  413. vars_of_md5 (void)
  414. {
  415.   Fprovide (Qmd5);
  416. }
  417.